<?php

namespace app\common;


use tool\Http;
use tool\Random;

class Sms
{
	
	/**
	 * 验证码有效时长
	 * @var int
	 */
	protected static $expire = 120;
	
	/**
	 * 最大允许检测的次数
	 * @var int
	 */
	protected static $maxCheckNums = 10;
	
	/**
	 * 获取最后一次手机发送的数据
	 *
	 * @param   int    $mobile 手机号
	 * @param   string $event  事件
	 * @return  Sms
	 */
	public static function get($mobile, $event = 'default')
	{
		$sms = \app\common\model\Sms::
		where(['mobile' => $mobile, 'event' => $event])
			->order('id', 'DESC')
			->find();
		Hook::listen('sms_get', $sms, null, true);
		return $sms ? $sms : null;
	}
	
	/**
	 * 发送验证码
	 *
	 * @param   int    $mobile 手机号
	 * @param   int    $code   验证码,为空时将自动生成6位数字
	 * @param   string $event  事件
	 * @return  boolean
	 */
	public static function send($mobile, $code = null, $event = 'default')
	{
	
        $code = is_null($code) ? Random::numeric() : $code;
		$time = time();
		$ip = request()->ip();
		$sms = \app\v1\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
        $username = '13592592467';
        $password = md5('lfl123456' . date('Y-m-d H:i:s'));
        $content = "【椰康国际】您的验证码是{$code}，该验证码五分钟之内有效！";
        $result = Http::post('http://api.sms1086.com/Api/Sendutf8.aspx', [
            'username' => $username,
            'password' => $password,
            'content' => $content,
            'mobiles' => $mobile,
            'timestamp' => date('Y-m-d H:i:s'),
            'f' => '1'
        ]);
		if (!$result) {
			$sms->delete();
			return false;
		}
		return true;
	}
	
	/**
	 * 发送通知
	 *
	 * @param   mixed  $mobile   手机号,多个以,分隔
	 * @param   string $msg      消息内容
	 * @param   string $template 消息模板
	 * @return  boolean
	 */
	public static function notice($mobile, $msg = '', $template = null)
	{
		$params = [
			'mobile'   => $mobile,
			'msg'      => $msg,
			'template' => $template
		];
		$result = Hook::listen('sms_notice', $params, null, true);
		return $result ? true : false;
	}
	
	/**
	 * 校验验证码
	 * @param   int    $mobile 手机号
	 * @param   int    $code   验证码
	 * @param   string $event  事件 登录=login,注册=register,换绑手机号=changemobile,更改密码=changepwd,重置密码=resetpwd
	 
	 
	 * @return  boolean
	 */
	public static function check($mobile, $code, $event = 'default')
	{
		$time = time() - self::$expire;
		$sms = \app\v1\model\Sms::where(['mobile' => $mobile, 'event' => $event])
			->order('id', 'DESC')
			->find();
		if ($sms) {
			if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) {
				$correct = $code == $sms['code'];
				if (!$correct) {
					$sms->times = $sms->times + 1;
					$sms->save();
					return false;
				} else {
					return true;
				}
			} else {
				// 过期则清空该手机验证码
				self::flush($mobile, $event);
				return false;
			}
		} else {
			return false;
		}
	}
	
	/**
	 * 清空指定手机号验证码
	 *
	 * @param   int    $mobile 手机号
	 * @param   string $event  事件
	 * @return  boolean
	 */
	public static function flush($mobile, $event = 'default')
	{
		\app\v1\model\Sms::where(['mobile' => $mobile, 'event' => $event])
			->delete();
		return true;
	}
}